package com.lyc.common.config;

import javax.annotation.Nonnull;
import javax.annotation.Resource;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.lyc.common.constants.CommonConstants;

/**
 * @Author 李大神仙
 * @Date 2024/3/18 19:45
 * @Version 1.0
 */
@Component
public class BeanPostProcessor implements ApplicationContextAware,org.springframework.beans.factory.config.BeanPostProcessor {

    @Resource
    private MybatusPlusConfig mybatusPlusConfig;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    }

    @Override
    public Object postProcessBeforeInitialization(@Nonnull Object bean, @Nonnull String beanName) throws BeansException {
        if (CommonConstants.BEAN_NAME_MYBATISPLUS.equals(beanName)){
            //如果遇到需要替换的bean,我们直接换成自已实现的bean即可（这里可以把旧的removeBeanDefinition,然后注册新的registerBeanDefinition）
            return mybatusPlusConfig.mybatusPlusInterceptor();
        }
        return bean;
    }
}
